<?php
class DefaultController extends FController {

    public function accessRules() {
        $config = array(
            'index' => 1
        );
        return ZRole::auth("space", $config);
    }

    public function __common() {
        parent::init();
        if (empty($this->cLoginUser)) {
          $this->redirect($this->createUrl('/site/login'));
        }

        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }
    
    /**
     * @ 首页列表
     * @ yindebin
     */
    public function actionIndex() {
        
        $this->__common();
        $eData = array();
        // 取考试项目 
        if($this->cLoginUser['cuser']['role_id'] != 1){
        	$esq = 'SELECT "school_id" FROM "{{user_school}}" WHERE "user_id" = '.$this->cLoginUser['cuser']['id'];
        	$edatas = Yii::app()->db->createCommand($esq)->queryRow();
        	
        	if(!empty($edatas)){
        		$examsql = 'SELECT "exam_id" FROM "{{exam_school}}" WHERE "school_id" IN ('.$edatas['school_id'].')';
        		$examdata = Yii::app()->db->createCommand($examsql)->queryAll();
        		
        		if(!empty($examdata)){
        			foreach ($examdata as $vale){
        				if(!empty($eData[$vale['exam_id']])){
        					$eData[$vale['exam_id']] = $eData[$vale['exam_id']];
        				}else{
							$eData[$vale['exam_id']] = ExamData::_getExam($vale['exam_id']);        				
        				}
        			}
        		}
        	}
        
        }else{
	        $exam = ExamData::_getExam();
	        foreach ($exam as $value){
	        	$eData[$value['exam_id']] = $value;
	        }
        }
        
        
        $where = "";
        $order = ' ORDER BY "update_time" DESC ';
        
        $where.= " AND A.\"notice_status\" = '1' AND A.\"expired\" >= '".strtotime(date("Y-m-d H:i:s",time()))."'";
        
        $select = "SELECT A.*,B.\"username\" FROM \"{{exam_notice}}\"  A";
        $sql = " LEFT JOIN \"{{user}}\" B ON A.\"user_id\" = B.\"id\"";
        $sql .= " WHERE 1=1".$where;
        
        $selectSql = $select.$sql.$order;
        
        $data = Yii::app()->db->createCommand($selectSql)->queryAll();
        
        
        foreach ($data as $value){
        	if(!empty($eData[$value['exam_id']])){
        		$eData[$value['exam_id']]['notice'][] = $value;
        	}
        }
        #$sqlCount = 'SELECT * FROM "{{exam_notice}}"  a '.$sql;
        #$data = ZCommon::getDaoPageList($selectSql, $sqlCount, 20);
        $this->render('index',array(
        	'data'	=>$eData,
        ));
    }
    
    public function actionInfo(){
    	$this->__common();
    	
    	$notice_id = Yii::app()->request->getParam('notice_id');

    	$notice_info = ExamNotice::model()->find('"notice_id" = :notice_id',array(':notice_id'=>$notice_id));
    
    	if(empty($notice_info)){
    		$this->redirect($this->createUrl('index'));
    	}
		$info = $notice_info->attributes;
    	$user_info = User::model()->findByPk($notice_info->user_id);
    
    	if(!empty($user_info)){
    		$info['username'] = $user_info->username;
    	}else{
    		$info['username'] = '用户信息不存在';
    	}
    	
    	$this->render('info',array(
    		'info'	=> $info,
    	));
    }

}